home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / include / user / spriteTime.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-24  |  4.7 KB  |  189 lines

  1. /*
  2.  * time.h --
  3.  *
  4.  *     External definitions for the time utility routines.
  5.  *
  6.  * Copyright 1986, 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  * rcsid: $Header: /user5/kupfer/spriteserver/include/user/RCS/spriteTime.h,v 1.3 92/04/23 23:56:18 kupfer Exp $ SPRITE (Berkeley)
  16.  */
  17.  
  18. #ifndef _SPRITETIME
  19. #define _SPRITETIME
  20.  
  21. #ifndef _SPRITE
  22. #include <sprite.h>
  23. #endif
  24.  
  25. /* DATA STRUCTURES */
  26.  
  27. /*
  28.  *  Definition of a time value.
  29.  *  Warning: if you change the definition of Time, be sure to update 
  30.  *  spriteTypes.defs.
  31.  */
  32.  
  33. typedef struct {
  34.     int    seconds;
  35.     int    microseconds;
  36. } Time;
  37.  
  38. typedef struct {
  39.     int year;
  40.     int month;
  41.     int dayOfYear;
  42.     int dayOfMonth;
  43.     int dayOfWeek;
  44.     int    hours;
  45.     int    minutes;
  46.     int    seconds;
  47.     int localOffset;
  48.     Boolean dst;
  49. } Time_Parts;
  50.  
  51. /* CONSTANTS */
  52.  
  53. /*
  54.  *  The number of microseconds in one second and one millisecond.
  55.  */
  56.  
  57. #define ONE_SECOND        1000000
  58. #define TENTH_SECOND        100000
  59. #define HUNDREDTH_SECOND    10000
  60. #define ONE_MILLISECOND        1000
  61.  
  62. /*
  63.  *  Length of buffers required by the Time conversion routines.
  64.  */
  65.  
  66. #define TIME_CVT_BUF_SIZE 30
  67.  
  68. /*
  69.  *  Frequently used time values.
  70.  */
  71.  
  72. extern Time time_ZeroSeconds;
  73. extern Time time_OneMicrosecond;
  74. extern Time time_OneMillisecond;
  75. extern Time time_TenMilliseconds;
  76. extern Time time_HundredMilliseconds;
  77. extern Time time_HalfSecond;
  78. extern Time time_OneSecond;
  79. extern Time time_TwoSeconds;
  80. extern Time time_TenSeconds;
  81. extern Time time_OneMinute;
  82. extern Time time_OneHour;
  83. extern Time time_OneDay;
  84. extern Time time_OneYear;
  85. extern Time time_OneLeapYear;
  86.  
  87.  
  88. /* PROCEDURES */
  89.  
  90. extern void    Time_Add _ARGS_((Time time1, Time time2, Time *resultPtr));
  91. extern void    Time_Subtract _ARGS_((Time time1, Time time2,
  92.                       Time *resultPtr));
  93. extern void    Time_Multiply _ARGS_((Time time, int factor, Time *resultPtr));
  94. extern void    Time_Divide _ARGS_((Time time, int factor, Time *resultPtr));
  95. extern void    Time_Normalize _ARGS_((Time *timePtr));
  96. extern void    Time_ToAscii _ARGS_((int time, Boolean relativeTime,
  97.                      char *bufferPtr));
  98. extern void    Time_ToParts _ARGS_((int time, Boolean relativeTime,
  99.                      Time_Parts *partsPtr));
  100.  
  101.  
  102. /*
  103.  *----------------------------------------------------------------------
  104.  *
  105.  * Time Comparisons --
  106.  *
  107.  *    Time_LT:    time1  <   time2
  108.  *    Time_LE:    time1  <=  time2
  109.  *    Time_EQ:    time1  ==  time2
  110.  *    Time_GE:    time1  >=  time2
  111.  *    Time_GT:    time1  >   time2
  112.  *
  113.  * Results:
  114.  *     TRUE    - the relation holds for the 2 values.
  115.  *     FALSE    - the relation does not hold.
  116.  *
  117.  * Side effects:
  118.  *     None.
  119.  *
  120.  *----------------------------------------------------------------------
  121.  */
  122.  
  123. #define Time_LT(time1, time2) \
  124.         (((time1).seconds     <  (time2).seconds) ||  \
  125.          (((time1).seconds     == (time2).seconds) &&  \
  126.           ((time1).microseconds <  (time2).microseconds)))
  127.  
  128. #define Time_LE(time1, time2) \
  129.         (((time1).seconds     <  (time2).seconds) ||  \
  130.          (((time1).seconds     == (time2).seconds) &&  \
  131.           ((time1).microseconds <= (time2).microseconds)))
  132.  
  133. #define Time_EQ(time1, time2) \
  134.         (((time1).seconds     == (time2).seconds) &&  \
  135.          ((time1).microseconds == (time2).microseconds))
  136.  
  137. #define Time_GE(time1, time2) \
  138.         (((time1).seconds     >  (time2).seconds) ||  \
  139.          (((time1).seconds     == (time2).seconds) &&  \
  140.           ((time1).microseconds >= (time2).microseconds)))
  141.  
  142. #define Time_GT(time1, time2) \
  143.         (((time1).seconds     >  (time2).seconds) ||  \
  144.          (((time1).seconds     == (time2).seconds) &&  \
  145.           ((time1).microseconds >  (time2).microseconds)))
  146.  
  147.  
  148. /*
  149.  *----------------------------------------------------------------------
  150.  *
  151.  * Time_ToMs --
  152.  *
  153.  *    Convert a time value (usually a small interval) to a floating-point 
  154.  *    number of milliseconds.
  155.  *
  156.  * Results:
  157.  *    Returns a float.
  158.  *
  159.  * Side effects:
  160.  *    None.
  161.  *
  162.  *----------------------------------------------------------------------
  163.  */
  164.  
  165. #define Time_ToMs(time)    ((float)(time).seconds * 1000 + \
  166.              (float)(time).microseconds / 1000)
  167.  
  168.  
  169. /*
  170.  *----------------------------------------------------------------------
  171.  *
  172.  * Time_Average --
  173.  *
  174.  *    Return the average time from some total.
  175.  *
  176.  * Results:
  177.  *    Returns the total time divided by the given count.  Returns 0 if 
  178.  *    the count is 0.  Be sure to cast this if total is a float.
  179.  *
  180.  * Side effects:
  181.  *    None.
  182.  *
  183.  *----------------------------------------------------------------------
  184.  */
  185.  
  186. #define Time_Average(total, count) ((count) == 0 ? 0 : (total) / (count))
  187.  
  188. #endif /* _SPRITETIME */
  189.